package data;

import java.util.*;
import commProtocol.*;

public class ArrayListAdapter implements CollectionContactTarget {

	private ArrayList<Contact> t;
	
	public ArrayListAdapter(){
		t = new ArrayList<Contact>();
	}
	
	public  void add(Contact contact){
		this.add(contact);
	}
	
	public void remove(String name){
		int position = 0;
		
		while((((Contact)(t.get(position))).getLogin() != name) && (position != t.size() -1)){
			position++;
		}
		try{
			if(((Contact) (t.get(position))).getLogin() == name)
				t.remove(t.get(position));
		}
		catch (Exception exp){
			System.out.println("contact inexistant");
		}
	}
	
	public void removeAll(){
		this.removeAll();
	}
	
	public Contact get(String name) {
		Contact c = new Contact("null", "null");
		int position = 0;
		
		while((((Contact)(t.get(position))).getLogin() != name) && (position != t.size() -1)){
			position++;
		}
		try{
			if(((Contact) (t.get(position))).getLogin() == name)
				c = (Contact) t.get(position);
		}
		catch (Exception exp){
			System.out.println("contact inexistant");
			c = null;

		}
		return c;
	}
	
	public Iterator<Contact> iterator(){
		return t.iterator();
	}
	
	
}
